var vm = new Vue({
    el: '#app',
    data: {
        host: host,
        token: sessionStorage.token || localStorage.token,
        error_old_pwd: false,
        error_pwd: false,
        error_pwd2: false,
        // error_old_pwd_message: '请填写当前密码',
        // error_pwd_message: '请填写密码',
        // error_pwd_message2: '两次密码不一致',
        old_password: '',
        password: '',
        password2: '',
    },
    methods: {
        // 检查数据
        check_password: function(){
            if (!this.old_password) {
                this.error_old_pwd = true;
            } else {
                this.error_old_pwd = false;
            }
        },
        check_pwd: function(){
            if (!this.password) {
                this.error_pwd = true;
            } else {
                this.error_pwd = false;
            }
        },

        check_pwd2: function(){
            if (this.password != this.password2) {
                this.error_pwd2 = true;
            } else {
                this.error_pwd2 = false;
            }
        },
        // 表单提交
        on_submit: function(){
            this.check_password();
            this.check_pwd();
            this.check_pwd2;
            console.log(this.error_old_pwd);
            console.log(this.error_pwd);
            console.log(this.error_pwd2);

            if (this.error_old_pwd == false && this.error_pwd == false && this.error_pwd2 == false) {
                axios.put(this.host + '/password/',
                    {
                        old_password: this.old_password,
                        new_password: this.password,
                        new_password2: this.password2
                    },
                    {
                        headers: {
                            'Authorization': 'JWT ' + this.token
                        },
                        responseType: 'json'
                    })
                    .then(response => {
                        alert("密码修改成功")
                        location.href = '/user_center_pass.html';
                    })
                    .catch(error => {
                        alert(response.data.err_msg)
                    })
            }
        },

    }
});